package fr.umlv.m2.uwp2011.service.user;

import javax.naming.AuthenticationException;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.ldap.core.LdapTemplate;

import fr.umlv.m2.uwp2011.service.user.UserDTO.Status;

public class SpringFrameworkLDAPClient {

	public static void main(String[] args) throws AuthenticationException {
		try {
			Resource resource = new ClassPathResource("fr/umlv/m2/uwp2011/service/user/uwp2011-service.xml");
			BeanFactory factory = new XmlBeanFactory(resource);

			UserDAO userDao = new LDAPUserDAO(
					(LdapTemplate) factory.getBean("ldapTemplate"));

			UserDTO userDto = userDao.getUserDetails("gboussy", "azerty");

			System.out.println(userDto.getGivenName());
			System.out.println(userDto.getLastName());
			System.out.println(userDto.getLogin());
			System.out.println(userDto.getMail());
			System.out.println(userDto.getStatus());
			System.out.println(userDto.getPassword());

//			 for (int i = 0; i < contactList.size(); i++) {
//			 System.out.println("Contact Name " + contactList.get(i));
//			 }
			 UserDTO newUserDTO = new UserDTO();
			
			 newUserDTO.setLogin("gboussy");
			 newUserDTO.setPassword("azerty");
			 newUserDTO.setGivenName("Guillaume");
			 newUserDTO.setLastName("Boussy");
			 newUserDTO.setMail("gboussy@student.com");
			 newUserDTO.setStatus(Status.selectStatus("student"));
			 
//			 userDao.insertUser(newUserDTO);
//			 userDao.updateUser(newUserDTO);
			 userDao.deleteUser(newUserDTO);
			 
		} catch (DataAccessException e) {
			System.out.println("Error occured " + e.getCause());
		}

		finally {
		}

	}
}
